package yunai.bio;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.security.SecureRandom;

/**
 * @Author: DRevelation
 * @Date: 2021/10/13 15:00
 */
@Slf4j
public class BIOClient {
    private static final Integer PORT = 8888;

    private static String IP_ADDRESS = "127.0.0.1";

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            clientReq(i);
        }
    }

    private static void clientReq(int i) {
        Socket socket = null;
        BufferedReader reader = null;
        PrintWriter writer = null;

        try {
            socket = new Socket(IP_ADDRESS, PORT);
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer = new PrintWriter(socket.getOutputStream());
            String[] operators = {"+", "-", "*", "/"};
            SecureRandom random = new SecureRandom();
            String expression = random.nextInt(10) + operators[random.nextInt(4)] + (random.nextInt(10) + 1);
            writer.println(expression);
            log.info("客户端【" + i + "】打印返回数据: " + reader.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != writer) {
                    writer.close();
                }
                if (null != socket) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
